#include <math.h>
#include <stdio.h>
#include <stdlib.h>


#define PI 3.1415926536

class DFT{
  private:
    double **cks;  // cos coefficients
    double **sks;  // sin coefficients
    bool init_ks();  // initialization of coefficients
    bool free_ks();  // frees memory
    int N;     // data set length. Default 16.
    int kmax;  // maximum k
  public:
    DFT(void);
    ~DFT(void);
    bool set_length_by_power(int power);
};

DFT::DFT() {
  N = 16;
  kmax = 8;

  //initialise ks
  cks = new double*[kmax];
  sks = new double*[kmax];

  for ( int k = 0; k <= kmax; k++ ) {
    cks[k] = new double [N];
    sks[k] = new double [N];
    for ( int i = 0; i < N-1; i++ ) {
      cks[k][i] = cos( 2 * PI * k * i / N );
      sks[k][i] = sin( 2 * PI * k * i / N );
    }
  }
  
};

DFT::~DFT() {
  free_ks();
}

bool DFT::set_length_by_power(int power) {
  free_ks();
  N = (int)pow(2,power);
  kmax = N / 2;
  printf("Data length: %i\n",N);
  init_ks();
  return true;
};

bool DFT::init_ks() {
  cks = new double*[kmax];
  sks = new double*[kmax];
  for ( int k = 0; k <= kmax; k++ ) {
    cks[k] = new double [N];
    sks[k] = new double [N];
    for ( int i = 0; i < N-1; i++ ) {
      cks[k][i] = cos( 2 * PI * k * i / N );
      sks[k][i] = sin( 2 * PI * k * i / N );
    }
  }
};

bool DFT::free_ks() {
  for ( int k = 0; k <= kmax; k++ ) {
    delete []cks[k];
  }
  delete []cks;
  for ( int k = 0; k <= kmax; k++ ) {
    delete []sks[k];
  }
  delete []sks;
  return true;
};


int main () {
  DFT dft;
  dft.set_length_by_power(8);
  return 1;
}

